package Thread;
/*
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

class MyThreadPool{
    private BlockingQueue<Runnable> queue = null;
    public MyThreadPool(int n) {
        // 初始化线程池，创建固定个数的线程
        // 这里使用ArrayBlockingQueue作为任务队列, 容量为1000
        queue = new ArrayBlockingQueue<>(1000);

        for(int i = 0; i < n; i++){
            Thread t = new Thread(() ->{
                while (true){
                    Runnable task = null;
                    try {
                        task = queue.take();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    task.run();
                }
            });
            //t.setDaemon(true);
            t.start();
        }
    }

    public void submit(Runnable task) throws InterruptedException {
        queue.put(task);
    }

}
public class demo1 {

    public static void main(String[] args) throws InterruptedException {
        MyThreadPool threadpool = new MyThreadPool(5);
        for(int i = 0; i < 100; i++){
            int id = i;
            threadpool.submit(() ->{
                System.out.println(Thread.currentThread().getName() + "id = " + id);
            });
        }
    }
}*/

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

class MyThreadPool{
    private BlockingQueue<Runnable> queue = null;
    public MyThreadPool(int n){
        queue = new ArrayBlockingQueue<>(1000);

        for(int i=0; i<n; i++){
            Thread t = new Thread(() -> {
                try{
                    while(true){
                        Runnable task = queue.take();
                        task.run();
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            });
            //t.setDaemon(true);
            t.start();
        }
    }

    public void submit(Runnable task) throws InterruptedException {
        queue.put(task);
    }
}

public class demo1{
    public static void main(String[] args) throws InterruptedException {
        MyThreadPool pool = new MyThreadPool(10);
        for(int i=0; i<1000; i++){
            int id =  i;
            pool.submit(() ->{
                System.out.println(Thread.currentThread().getName() + " id = " + id);
            });
        }
    }
}


















